package com.charles.recordapp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentViewHolder;

import static android.content.ContentValues.TAG;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.charles.recordapp.R.layout.me_header;

public class MeFragment extends Fragment {
    private ListView listView;
    private Map<String, Object> map;
    private ArrayList<Map> data;
    private MeAdapter meAdapter;

    @SuppressLint("ResourceAsColor")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.me_fragment, container, false);
        data = new ArrayList<Map>();
        for (int i = 0; i < 5; i++) {
            map = new HashMap<>();
            map.put("imageView", "image" + i);
            map.put("nameText", "视频名称" + i);
            map.put("sizeText", "20.5" + i + "M");
            map.put("dateText", "2021-10-10 20:08:30" + i);
            map.put("longText", "12:0" + i);
            data.add(map);
        }

        meAdapter = new MeAdapter();
        meAdapter.tempData = data;
        meAdapter.context = getContext();
        listView = view.findViewById(R.id.listView);
        //listView.setBackgroundColor(R.color.red);
        listView.setAdapter(meAdapter);
        View header = inflater.inflate(R.layout.me_header, null);
        listView.addHeaderView(header);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    class MeAdapter extends BaseAdapter {
        private ArrayList<Map> tempData;
        private Context context;

        public void setTempData(ArrayList<Map> data) {
            this.tempData = data;
        }

        public void setContext(Context context) {
            this.context = context;
        }

        @Override
        public int getCount() {
            return tempData.size();
        }

        @Override
        public Object getItem(int position) {
            return tempData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @SuppressLint("ResourceAsColor")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.me_item, null);
                holder = new Holder();
                holder.viewBg = convertView.findViewById(R.id.viewBg);
                holder.imageView = convertView.findViewById(R.id.imagePre);
                holder.nameText = convertView.findViewById(R.id.nameText);
                holder.sizeText = convertView.findViewById(R.id.sizeText);
                holder.longText = convertView.findViewById(R.id.longText);
                holder.dateText = convertView.findViewById(R.id.dateText);
                holder.buttonOutput = convertView.findViewById(R.id.outPutBtn);
                holder.buttonEdit = convertView.findViewById(R.id.editBtn);
                holder.buttonMore = convertView.findViewById(R.id.more);
                holder.moreView = convertView.findViewById(R.id.moreView);
                holder.itemdelete = convertView.findViewById(R.id.itemDeleted);
                holder.itemRename = convertView.findViewById(R.id.itemRename);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            Map tempMap = (Map) tempData.get(position);
            holder.imageView.setImageDrawable(getResources().getDrawable(R.mipmap.avatar1, null));
            holder.nameText.setText(tempMap.get("nameText") + "");
            holder.sizeText.setText(tempMap.get("sizeText") + "");
            holder.dateText.setText(tempMap.get("dateText") + "");
            holder.longText.setText(tempMap.get("longText") + "");
            ViewGroup vp = (ViewGroup) convertView;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                if (viewchild instanceof Button || viewchild instanceof ImageButton) {
                    viewchild.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            clickAction(viewchild, holder, position);
                        }
                    });
                }
            }
            return convertView;
        }

        public void clickAction(View button, Holder holder, int position) {
            //按钮事件处理
            if (button == holder.buttonOutput) {
                //点击了编辑按钮
                Log.d("TAG", String.format("<----------点击了第%d行的导出按钮---------->", position + 1));
            } else if (button == holder.buttonEdit) {
                //点击了编辑
                Intent intent = new Intent(getActivity(), MeVideoEditActivity.class);
                intent.putExtra("path", "MeVideoEditActivity");
                startActivityForResult(intent,1000);
                Log.d("TAG", String.format("<----------点击了第%d行的编辑按钮---------->", position + 1));
            } else if (button == holder.itemdelete || button == holder.itemRename || button ==  holder.buttonMore) {
                holder.moreView.setVisibility(holder.moreView.getVisibility() == INVISIBLE ? VISIBLE : INVISIBLE);
                holder.itemdelete.setVisibility(holder.moreView.getVisibility());
                holder.itemRename.setVisibility(holder.moreView.getVisibility());
                if (button == holder.itemdelete) {
                    //点击了删除
                    Log.d("TAG", String.format("<----------点击了第%d行的删除按钮---------->", position + 1));
                } else if (button == holder.itemRename) {
                    //点击了重命名
                    Log.d("TAG", String.format("<----------点击了第%d行的重命名按钮---------->", position + 1));
                } else  {
                    //点击了更多按钮
                    Log.d("TAG", String.format("<----------点击了第%d行的更多按钮---------->", position + 1));
                }

            }
        }
    }

    public class Holder {
        Button buttonEdit;
        Button buttonOutput;
        ImageButton buttonMore;
        ImageView viewBg;
        ImageView imageView;
        TextView nameText;
        TextView sizeText;
        TextView dateText;
        TextView longText;
        View moreView;
        Button itemRename;
        Button itemdelete;
    }
}
